package org.itboys.mobile.service.mongo.weixin;


import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProject;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProjectWord;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：众包_微信_留言_service。支持留言下留言
 */
@Service
public class ZhongBaoWeiXinProjectWordService extends BaseMongoService<ZhongBaoProjectWord> {

    @Autowired
    private SundayWeiXinMemberService memberService;
    @Autowired
    private ZhongBaoWeiXinProjectService projectService;
    @Autowired
    private ZhongBaoWeiXinMessageService messageService;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<ZhongBaoProjectWord> getEntityClass() {
        return ZhongBaoProjectWord.class;
    }


    /**
     *  发布或回复留言
     * @param projectId
     * @param memberId
     * @param words
     *@param oldWordId 原留言ID
     * @param messageId 消息通知的ID
     * 1,情况：1，留言。(项目发布人留言、其他人留言)，2回复留言
     */
    public void submit(Long projectId,Long memberId,String words,Long oldWordId,Long messageId){
        SundayMember member=memberService.getById(memberId);
        ZhongBaoProject project=projectService.getById(projectId);
        boolean isSendMessage=true;
        //留言标题
      //  String nickName=member.getNickName();
        //留言的顶级父级ID
        Long topParentId=0l;
        //消息的归属人。
        Long messageOwnerId=project.getMemberId();
       //1，回复留言，需要生成推送消息需要生成留言
        if(LongUtil.isNotZreo(oldWordId)){
            ZhongBaoProjectWord oldWord=super.getById(oldWordId);
            topParentId=oldWord.getTopParentId();
          //  nickName=member.getNickName()+"[回复]"+oldWord.getMemberName();
            messageOwnerId=oldWord.getMemberId();

        //2，直接留言(1,用户留言，2项目发布者留言)
        }else{

            //2.1,项目发布者直接留言。不需要推送消息，需要生成留言
            if(project.getMemberId().longValue()==memberId.longValue()){
                isSendMessage=false;
            //2.2，其他人员留言。需要推送消息，需要生成留言
            }

        }
        //生成留言
        ZhongBaoProjectWord word =new ZhongBaoProjectWord(projectId,  memberId, member.getNickName(),  member.getMobile(),  member.getImage(), words,oldWordId,topParentId);
        super.save(word);


        //原留言ID。就是本次生成留言的ID,
        oldWordId=word.getId();
        /*}else{

        }*/

        //生成消息通知
        if(isSendMessage){
            messageService.generateMessage(messageOwnerId,memberId,projectId,SundayCommonConstants.zhongbao_message_type_word,"项目留言",words,oldWordId,messageId);
        }
         //设置原消息通知为已读
/*
        if(LongUtil.isNotZreo(messageId)){
            messageService.read(messageId,null);
        }
*/

    }
    /**
     * 获取项目的分页
     * @param request
     * @param projectId
    // * @param topParentId
     * @return
     */
    public List<ZhongBaoProjectWord> getData(HttpServletRequest request, Long projectId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        return super.pageQuery(request,param).getData();
    }

}
